package com.skyline.common.tag;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.skyline.wo.model.ShortMessage;

public class ShortMessageContentTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -526878710143426134L;

	private ShortMessage shortMessage;
	
	private String skylineDomain="/skyline";

	//private String userWoUrl;

	private static final Log LOGGER = LogFactory.getLog(ShortMessageContentTag.class);

	private String[] smileys = { "angel_smile", "angry_smile", "broken_heart", "confused_smile", "cry_smile", "devil_smile",
			"embaressed_smile" };
	private String[] smileysChn = { "天使", "生气", "心碎", "迷惑", "哭泣", "恶魔", "尴尬" };

	private String smileysPath = "/plugin/ckeditor/plugins/smiley/images/";

	private String getText(String text) {
		String getText = text;
		for (int i = 0; i < smileysChn.length; i++) {
			String reg = "/" + smileys[i];
			String rep = "<img lt='" + smileysChn[i] + "' src='" + skylineDomain+smileysPath + smileys[i] + ".gif'>";
			getText = getText.replaceAll(reg, rep);
		}
		return getText;
	}

	private String genContent() {

		Long sourceOwnerId = shortMessage.getSourceOwnerId();
		String sourceOwnerNickName = shortMessage.getSourceOwnerNickname();
		String comment = shortMessage.getComment();
		String text = getText(shortMessage.getText());
		
		//输出各个评论
		if (sourceOwnerId != null && sourceOwnerNickName != null && comment != null) {
			StringBuilder sb = new StringBuilder("");
			sb.append(getText(comment));
			List<String> comments=shortMessage.getComments();
			List<String> commentterNicknames=shortMessage.getCommetterNicknames();
			List<Long> commentterIds=shortMessage.getCommetterIds();
			if(comments!=null&&comments.size()>0){
				int size=comments.size();
				for(int i=0;i<size;i++){
					String curComment=comments.get(i);
					Long curCommentterId=commentterIds.get(i);
					String curCommetterNickname=commentterNicknames.get(i);
					sb.append(" || ");
					genCommenterLink(sb,curCommentterId,curCommetterNickname);
					sb.append(":");
					sb.append(getText(curComment));
				}
			}
			sb.append("<div>");
			genCommenterLink(sb,sourceOwnerId,sourceOwnerNickName);
			sb.append(":<br>");
			sb.append(text);
			sb.append("</div>");
			return sb.toString();
		} else {
			return text;
		}

	}

	private void genCommenterLink(StringBuilder sb ,Long commentterId,String commentterNickname){
		sb.append("<a href='");
		sb.append(skylineDomain+"/wo/");
		sb.append(commentterId);
		sb.append(".html'>");
	sb.append(commentterNickname);
	sb.append("</a>");
	}
	
	
	@Override
	public int doEndTag() throws JspException {
		try {
			pageContext.getOut().println(genContent());
		} catch (IOException e) {
			LOGGER.warn("调用ShortMessageContentTag出错", e);
		}
		return EVAL_BODY_INCLUDE;
	}

	public ShortMessage getShortMessage() {
		return shortMessage;
	}

	public void setShortMessage(ShortMessage shortMessage) {
		this.shortMessage = shortMessage;
	}


	public String getSkylineDomain() {
		return skylineDomain;
	}

	public void setSkylineDomain(String skylineDomain) {
		this.skylineDomain = skylineDomain;
	}

	public String getSmileysPath() {
		return smileysPath;
	}

	public void setSmileysPath(String smileysPath) {
		this.smileysPath = smileysPath;
	}

}
